package com.csw.android.player.view.video

import android.content.Context
import android.hardware.SensorManager
import android.util.AttributeSet
import android.view.OrientationEventListener
import androidx.lifecycle.LifecycleOwner
import com.csw.android.player.base.AbsMediaControllerView
import com.csw.android.player.base.MediaInfo
import com.csw.android.player.utils.ScreenOrientationUtils
import com.csw.android.player.view.controller.MediaControllerView

class VerticalScrollVideoView(context: Context, attrs: AttributeSet? = null) :
    VideoView(context, attrs) {

    private val orientationEventListener =
        object : OrientationEventListener(context, SensorManager.SENSOR_DELAY_UI) {
            private var uiGravity: ScreenOrientationUtils.UIGravity? = null
            private var changeMinAngle = 60

            override fun onOrientationChanged(orientation: Int) {
                if (orientation < 0) {
                    return
                }
                uiGravity?.run {
                    when (this) {
                        ScreenOrientationUtils.UIGravity.RIGHT -> {
                            if (orientation in (90 - changeMinAngle)..(90 + changeMinAngle)) {
                                return
                            }
                        }

                        ScreenOrientationUtils.UIGravity.TOP -> {
                            if (orientation in (180 - changeMinAngle)..(180 + changeMinAngle)) {
                                return
                            }
                        }

                        ScreenOrientationUtils.UIGravity.LEFT -> {
                            if (orientation in (270 - changeMinAngle)..(270 + changeMinAngle)) {
                                return
                            }
                        }

                        ScreenOrientationUtils.UIGravity.BOTTOM -> {
                            if (orientation in 0..(0 + changeMinAngle) || orientation in (360 - changeMinAngle)..360) {
                                return
                            }
                        }
                    }
                }
                ScreenOrientationUtils.orientationToUIGravity(orientation).run {
                    if (uiGravity != this) {
                        uiGravity = this
                        handleOrientationChange(this)
                    }
                }
            }

            fun handleOrientationChange(orientation: ScreenOrientationUtils.UIGravity) {
                playerInfo?.run {
                    if (buttonPlaying
                        && (orientation == ScreenOrientationUtils.UIGravity.RIGHT
                                || orientation == ScreenOrientationUtils.UIGravity.LEFT)
                    ) {
                        if (videoHeight > videoWidth) {
                            startFullScreenPlay(ScreenOrientationUtils.UIGravity.BOTTOM)
                        } else {
                            startFullScreenPlay(orientation)
                        }
                    }
                }

            }
        }

    init {
        mGestureView.verticalGestureEnable = false
        mGestureView.transformGestureEnable = false
    }

    override fun createMediaController(context: Context): AbsMediaControllerView? {
        return MediaControllerView(context)
    }

    override var mediaInfo: MediaInfo?
        get() = super.mediaInfo
        set(value) {
            if (super.mediaInfo != value) {
                super.mediaInfo = value
                mPreviewView.show()
            }
        }

    override fun onResume(owner: LifecycleOwner) {
        super.onResume(owner)
        orientationEventListener.enable()
    }

    override fun onPause(owner: LifecycleOwner) {
        orientationEventListener.disable()
        super.onPause(owner)
    }
}